/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.ui.form;

import com.rcnss.data.User;
import com.rcnss.ui.form.field.factory.UserFieldFactory;
import com.rcnss.ui.table.handler.GeneralActionHandler;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window.Notification;



/**
 *
 * @author Guesmi
 */
public class UsersListForm extends AbstractForm {
    public Table table = new Table();
    public ItemClickListener itemClickListener = new ItemClickListener() {
            public void itemClick(ItemClickEvent event) {
                if(event.getButton()==ItemClickEvent.BUTTON_LEFT)
                {
                    Object id = event.getItemId();
                    if(id == null)
                    {
                        splitPanel.setSecondComponent(null);
                        return;
                    }
                    EditForm f = new EditForm(User.SearchByID((Integer)id),
                            new UserFieldFactory());
                    //f.setVisibleItemProperties(formVisibleCols);
                    splitPanel.setSecondComponent(f);
                    removalButton.setVisible(id != null);
                }
            }
        };
    private IndexedContainer userContainer = new IndexedContainer();
    private Button removalButton;
    private static String[] formVisibleCols = {
        "Matricule", "Nom", "Prenom", "Tel. Bureau", "Tel. GSM"};

    public UsersListForm()
    {
        initTop();
        initLayout();
        initTable();
       // this.splitPanel.setSplitPosition(80);
    }

    private void initTable()
    {


        this.table.setWidth("100%");
        this.table.setPageLength(18);
        // selectable
        table.setSelectable(true);
        table.setMultiSelect(false);
        table.setImmediate(true); // react at once when something is selected
        // connect data source
        initContainer();
        table.setContainerDataSource(this.userContainer);
        // turn on column reordering and collapsing
        this.table.setColumnReorderingAllowed(true);
        this.table.setColumnCollapsingAllowed(true);
        // set column headers
        this.table.addActionHandler(new GeneralActionHandler());
        
        this.table.addListener(itemClickListener);

        left.addComponent(this.top);
        left.addComponent(this.table);
        this.top.setWidth("100%");
        this.splitPanel.setFirstComponent(left);
    }

    private void initTop()
    {
        top.addComponent(new Button("+",
                new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                Object id = table.addItem();
                table.setValue(id);
            }
        }));

        // Remove item button
        removalButton = new Button("-", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                table.removeItem(table.getValue());
                table.select(null);
            }
        });
        removalButton.setVisible(false);
        top.addComponent(removalButton);
        for(final String pn : formVisibleCols)
        {
            final TextField sf = new TextField();
            this.top.addComponent(sf);
            sf.setWidth("100%");
            sf.setInputPrompt(pn);
            sf.setImmediate(true);
            this.top.setExpandRatio(sf, 1);
            sf.addListener(new Property.ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
                    userContainer.removeContainerFilters(pn);
                    if (sf.toString().length() > 0 && !pn.equals(sf.toString())) {
                        userContainer.addContainerFilter(pn, sf.toString(),
                                true, false);
                    }
                    getWindow().showNotification(
                            "" + userContainer.size() + " champs trouves",
                            Notification.TYPE_TRAY_NOTIFICATION);
                }
            });
        }
    }
    private void initContainer()
    {
        this.userContainer.addContainerProperty("Matricule", String.class, null);
        this.userContainer.addContainerProperty("Nom", String.class, null);
        this.userContainer.addContainerProperty("Prenom", String.class, null);
        this.userContainer.addContainerProperty("Tel. Bureau", String.class, null);
        this.userContainer.addContainerProperty("Tel. GSM", String.class, null);
        int i =0;
        for(User u : User.GetAll())
        {
            Item item = this.userContainer.addItem(u.getID());
            item.getItemProperty("Nom").setValue(u.getNOM());
            item.getItemProperty("Prenom").setValue(u.getPrenom());
            item.getItemProperty("Matricule").setValue(u.getMatricule());
            item.getItemProperty("Tel. Bureau").setValue(u.getTelBureau());
            item.getItemProperty("Tel. GSM").setValue(u.getTelMobile());
        }
    }
}
